package br.com.wss.recursos;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

public class Conexao {
	private InputStream stream;
	private String url;

	public Conexao(String url) {
		this.url = url;
	}

	public InputStream getConecao() throws MalformedURLException {
		int response = -1;
		try {

			URL url = new URL(this.url);
			URLConnection conn = url.openConnection();

			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setAllowUserInteraction(false);
			httpConn.setInstanceFollowRedirects(true);
			httpConn.setRequestMethod("GET");
			httpConn.connect();
			response = httpConn.getResponseCode();
			if (response == HttpURLConnection.HTTP_OK) {
				stream = httpConn.getInputStream();
			}
		} catch (MalformedURLException e) {
			System.out.println("Erro na url: " + e);
		} catch (ProtocolException e) {
			System.out.println("Erro protocolo: " + e);
		} catch (IOException e) {
			System.out.println("Erro: " + e);
		}

		return stream;
	}
}
